﻿using Jacobi.ComponentModel.Decomposition;
using Jacobi.ComponentModel.Decomposition.Test.Stubs;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using System;
using System.Threading;

namespace Jacobi.ComponentModel.Decomposition.Test
{
    /// <summary>
    ///This is a test class for PipesAndFiltersEngineTest and is intended
    ///to contain all PipesAndFiltersEngineTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PipesAndFiltersEngineTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Start
        ///</summary>
        [TestMethod()]
        public void StartTest()
        {
            OperationCollection operations = CreatePipeline();
            PipesAndFiltersEngine target = new PipesAndFiltersEngine(operations);
            target.Start();

            Thread.Sleep(500);

            target.Cancel();
        }

        private OperationCollection CreatePipeline()
        {
            // create filters
            RandomFilter rndFilter1 = new RandomFilter("Rnd1");
            RandomFilter rndFilter2 = new RandomFilter("Rnd2");
            SlowFilter slowFilter1 = new SlowFilter("Slow1");
            SlowFilter slowFilter2 = new SlowFilter("Slow2");
            SlowFilter slowFilter3 = new SlowFilter("Slow3");

            // connect filters
            Connect(rndFilter1, slowFilter1);
            Connect(rndFilter1, slowFilter1);

            Connect(rndFilter2, slowFilter2);
            Connect(rndFilter2, slowFilter2);

            Connect(slowFilter1, slowFilter3);
            Connect(slowFilter1, slowFilter3);
            Connect(slowFilter2, slowFilter3);
            Connect(slowFilter2, slowFilter3);

            // add filters to collection
            OperationCollection operations = new OperationCollection();
            operations.Add(rndFilter1);
            operations.Add(rndFilter2);
            operations.Add(slowFilter1);
            operations.Add(slowFilter2);
            operations.Add(slowFilter3);

            return operations;
        }

        private void Connect(IFilter<PipeData, PipeData> provider, IFilter<PipeData, PipeData> consumer)
        {
            Pipe<PipeData> pipe = new Pipe<PipeData>();

            provider.Outputs.Add(pipe.Producer);
            consumer.Inputs.Add(pipe.Consumer);
        }
    }
}
